﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Shell.Settings;
using System.IO;
using Microsoft.VisualStudio.Settings;

namespace VsxFactory.ExtensionManager
{
    internal class PackageSettings : IPackageSourceSettingsManager
    {
        private const string PackageSourcesSettingProperty = "PackageSources";
        private const string ActivePackageSourceSettingProperty = "ActivePackageSource";
        private const string SettingsRoot = "VsxFactory.PrivateExtensionManager";

        private WritableSettingsStore _userSettingsStore;
        private IServiceProvider _serviceProvider;

        /// <summary>
        /// Initializes a new instance of the <see cref="SettingsService"/> class.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        public PackageSettings(IServiceProvider serviceProvider)
        {
            System.Diagnostics.Contracts.Contract.Requires(serviceProvider != null);

            _serviceProvider = serviceProvider;
        }

        private WritableSettingsStore UserSettingsStore
        {
            get
            {
                if (_userSettingsStore == null)
                {
                    var settingsManager = new ShellSettingsManager(_serviceProvider);
                    _userSettingsStore = settingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);

                    // Ensure that the package collection exists before any callers attempt to use it.
                    if (!_userSettingsStore.CollectionExists(SettingsRoot))
                    {
                        _userSettingsStore.CreateCollection(SettingsRoot);
                    }
                }
                return _userSettingsStore;
            }
        }

        public string ActivePackageSourceString
        {
            get
            {
                return UserSettingsStore.GetString(SettingsRoot, ActivePackageSourceSettingProperty, null);
            }
            set
            {
                _userSettingsStore.SetString(SettingsRoot, ActivePackageSourceSettingProperty, value ?? String.Empty);
            }
        }

        public bool IsFirstRunning
        {
            get
            {
                return PackageSourcesString != null;
            }
            set
            {
            }
        }

        public string PackageSourcesString
        {
            get
            {
                return UserSettingsStore.GetString(SettingsRoot, PackageSourcesSettingProperty, null);
            }
            set
            {
                UserSettingsStore.SetString(SettingsRoot, PackageSourcesSettingProperty, value ?? String.Empty);
            }
        }


        public IEnumerable<PackageSource> PackageSources
        {
            get
            {
                var sources = PackageSourcesString;
                if (!String.IsNullOrEmpty(sources))
                {
                    foreach (var part in sources.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        var segments = part.Split('=');
                        yield return new PackageSource(segments[0], segments[1]);
                    }
                }
            }
        }
    }
}